package com.cmcc.xdr.util;

import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Arrays;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 
 */

/**
 * @author lvpin
 *
 */
public class JudgeBytes {
	private static Logger log = LogManager.getLogger(JudgeBytes.class);
	
	static byte[] bytes1 = hexStringToBytes("ff");
	static byte[] bytes2 = hexStringToBytes("ffff");
	static byte[] bytes3 = hexStringToBytes("ffffff");
	static byte[] bytes4 = hexStringToBytes("ffffffff");
	static byte[] bytes8 = hexStringToBytes("ffffffffffffffff");
	static byte[] bytes16 = hexStringToBytes("ffffffffffffffffffffffffffffffff");
	static byte[] bytes24 = hexStringToBytes("ffffffffffffffffffffffffffffffffffffffffffffffff");
	static byte[] bytes32 = hexStringToBytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
	static byte[] bytes64 = hexStringToBytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
	static byte[] bytes128 = hexStringToBytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//生成测试数据
		byte[] bytes1temp = hexStringToBytes("ff");
		byte[] bytes2temp = hexStringToBytes("fff8");
		byte[] bytes3temp = hexStringToBytes("ffffff");
		byte[] bytes4temp = hexStringToBytes("ffffffff");
		byte[] bytes8temp = hexStringToBytes("ffffffffffffffff");
		byte[] bytes16temp = hexStringToBytes("ffffffffffffffffffffffffffffffff");
		byte[] bytes24temp = hexStringToBytes("ffffffffffffffffffffffffffffffffffffffffffffffff");
		byte[] bytes32temp = hexStringToBytes("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1");
		
		//判断各个字节是否为全F（全1）
		log.debug(judgeBytesIfF(bytes1temp));log.debug(judgeBytesIfFdefault(bytes1temp,1));
		log.debug(judgeBytesIfF(bytes2temp));log.debug(judgeBytesIfFdefault(bytes2temp,2));
		log.debug(judgeBytesIfF(bytes3temp));log.debug(judgeBytesIfFdefault(bytes3temp,3));
		log.debug(judgeBytesIfF(bytes4temp));log.debug(judgeBytesIfFdefault(bytes4temp,4));
		log.debug(judgeBytesIfF(bytes8temp));log.debug(judgeBytesIfFdefault(bytes8temp,8));
		log.debug(judgeBytesIfF(bytes16temp));log.debug(judgeBytesIfFdefault(bytes16temp,16));
		log.debug(judgeBytesIfF(bytes24temp));log.debug(judgeBytesIfFdefault(bytes24temp,24));
		log.debug(judgeBytesIfF(bytes32temp));log.debug(judgeBytesIfFdefault(bytes32temp,32));
		
		long start = System.currentTimeMillis();
		for(int i =0 ;i<10000000; i++){
			//log.debug(judgeBytesIfF(bytes32));
			judgeBytesIfF(bytes32temp);
		}		
		long stop = System.currentTimeMillis();
		log.debug(stop-start);
		
		long start2 = System.currentTimeMillis();
		for(int i =0 ;i<10000000; i++){
			//log.debug(judgeBytesIfF(bytes32));
			judgeBytesIfFdefault(bytes32temp,32);
		}		
		long stop2 = System.currentTimeMillis();
		log.debug(stop2-start2);
	}
	
	public static boolean judgeBytesIfF(byte[] bytes){
		boolean boo = true;
		for(byte b : bytes){
		Integer temp  = new Integer(b);
			//log.debug(temp);
			if(temp != -1){
				boo = false;
				break;
			}
		}
		return boo;
	}
	
	public static boolean judgeBytesIfFdefault(byte[] bytes,int bytesnum){
		switch(bytesnum){
		    case 1: return Arrays.equals(bytes, bytes1);			           
		    case 2: return Arrays.equals(bytes, bytes2);
		    case 3: return Arrays.equals(bytes, bytes3);
		    case 4: return Arrays.equals(bytes, bytes4);		    	
		    case 8: return Arrays.equals(bytes, bytes8);
		    case 16: return Arrays.equals(bytes, bytes16);
		    case 24: return Arrays.equals(bytes, bytes24);    	
		    case 32: return Arrays.equals(bytes, bytes32);    	
		    case 64: return Arrays.equals(bytes, bytes64);    	
		    case 128: return Arrays.equals(bytes, bytes128);		    
		    default:return false;
		}
	}
	
	public static byte[] hexStringToBytes(String hexString) {
	    if (hexString == null || hexString.equals("")) {
	        return null;
	    }
	    hexString = hexString.toUpperCase();
	    int length = hexString.length() / 2; //两个十六进制字符转换成一个byte
	    char[] hexChars = hexString.toCharArray();
	    byte[] d = new byte[length];
	    for (int i = 0; i < length; i++) {
	        int pos = i * 2;
	        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); //复用charToByte(char c)方法
	    }
	    return d;
	}
	
	private static byte charToByte(char c) {
	    return (byte) "0123456789ABCDEF".indexOf(c);
	}
	
	public static BigInteger toBigInteger(byte[] arr) {
		// 在原数组前插入一字节作为符号位
		byte[] result = new byte[arr.length + 1];
		for (int i = arr.length; i > 0; i--) {
			result[i] = arr[i - 1];
		}
		return new BigInteger(result);
	}
	
	/**
	 * 字节数组转换为double
	 * 
	 * @param data
	 * @return
	 */
	public static double toDouble(byte[] data) {
		return ByteBuffer.wrap(data).getDouble();
	}

	public static String toIpAddress(byte[] bytes) {
		if (bytes[0] == -1 && bytes.length > 4) {
			bytes = Arrays.copyOfRange(bytes, bytes.length - 4, bytes.length);
		}
		try {
			return InetAddress.getByAddress(bytes).getHostAddress();
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		}
	}

}